
package net.lab0.fractal.gui;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.lab0.fractal.classes.ViewCoordinates;
import net.lab0.fractal.fractals.Mandelbrot;


/**
 * @author  116
 */
public class MandelbrotPanel
extends JPanel
{
    /**
	 * @uml.property  name="mandelbrot"
	 * @uml.associationEnd  
	 */
    private Mandelbrot mandelbrot;
    /**
	 * @uml.property  name="fractal2dView"
	 * @uml.associationEnd  
	 */
    private Fractal2dView fractal2dView;
    
    /**
     * Create the panel.
     */
    public MandelbrotPanel()
    {
        ViewCoordinates coords = new ViewCoordinates(0.0, 0.0, 2.0, 2.0);
        mandelbrot = new Mandelbrot();
        mandelbrot.setMaxIterations(1024);
        
        setLayout(new BorderLayout(0, 0));
        
        fractal2dView = new Fractal2dView(mandelbrot, coords);
        add(fractal2dView, BorderLayout.CENTER);
        
        JPanel commandPanel = new JPanel();
        add(commandPanel, BorderLayout.WEST);
        GridBagLayout gbl_commandPanel = new GridBagLayout();
        gbl_commandPanel.columnWidths = new int[] { 79, 0, 0 };
        gbl_commandPanel.rowHeights = new int[] { 0, 0, 0, 0 };
        gbl_commandPanel.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
        gbl_commandPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
        commandPanel.setLayout(gbl_commandPanel);
        
        JLabel lblMandelbrot = new JLabel("Mandelbrot");
        GridBagConstraints gbc_lblMandelbrot = new GridBagConstraints();
        gbc_lblMandelbrot.fill = GridBagConstraints.VERTICAL;
        gbc_lblMandelbrot.gridwidth = 2;
        gbc_lblMandelbrot.insets = new Insets(0, 0, 5, 0);
        gbc_lblMandelbrot.gridx = 0;
        gbc_lblMandelbrot.gridy = 0;
        commandPanel.add(lblMandelbrot, gbc_lblMandelbrot);
        
        Component horizontalStrut = Box.createHorizontalStrut(70);
        GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
        gbc_horizontalStrut.insets = new Insets(0, 0, 5, 0);
        gbc_horizontalStrut.gridx = 1;
        gbc_horizontalStrut.gridy = 1;
        commandPanel.add(horizontalStrut, gbc_horizontalStrut);
        
        JLabel lblIterations = new JLabel("Iterations");
        GridBagConstraints gbc_lblIterations = new GridBagConstraints();
        gbc_lblIterations.insets = new Insets(0, 0, 0, 5);
        gbc_lblIterations.gridx = 0;
        gbc_lblIterations.gridy = 2;
        commandPanel.add(lblIterations, gbc_lblIterations);
        
        JSpinner spinner = new JSpinner();
        spinner.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                if (e.getSource() instanceof JSpinner)
                {
                    JSpinner s = (JSpinner) e.getSource();
                    updateIteration(((SpinnerNumberModel)s.getModel()).getNumber());
                }
            }
        });
        spinner.setModel(new SpinnerNumberModel(1024, 256, 65536, 256));
        GridBagConstraints gbc_spinner = new GridBagConstraints();
        gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
        gbc_spinner.gridx = 1;
        gbc_spinner.gridy = 2;
        commandPanel.add(spinner, gbc_spinner);
    }

    private void updateIteration(Number number)
    {
        mandelbrot.setMaxIterations(number.intValue());
        fractal2dView.forceRepaint();
    }

    @Override
    public String getName()
    {
        return "Mandelbrot";
    }
    
    
}
